<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include_once(APPPATH."libraries/AdminController.php");
class Form_report extends AdminController {  
	
	var $status_form = "'APPROVE','DONE'";
	var $dis = "";
	var $is_detail = FALSE;
	
	function __construct()   
	{
		parent::__construct();    
		$this->_set_action();
		$this->_set_action(array("detail"),"ITEM");
		$this->cat_search = array(
			'all'						=> 'Pilih Kategory',
			'form_principal_name'		=> 'Nama Principal',
			'form_obligee_name'			=> 'Nama Obligee',
			'form_bank_garansi_type'	=> 'Tipe Bank Garansi'
			/*'form_obligee_address'		=> 'Alamat Obligee',			
			'form_principal_address'	=> 'Alamat Principal',
			'form_document_number'		=> 'Nomor Dokumen',
			'form_document_desc'		=> 'Penjelasan Dokumen',
			'marketing_name'			=> 'Nama Marketing',
			'cur_name'					=> 'Nama Mata Uang'*/
		);
		$this->_set_title('Report > Laporan Formulir Asuransi');
		$this->_set_cat_search(array("all"	=> $this->cat_search));	
		$this->DATA->table="tm_formulir";
		$this->load->model("mdl_formulir_report","A");
		$this->folder_view = "report/";
		$this->prefix_view = strtolower($this->_getClass());

	}

	function index(){
		
		$mst=isset($this->jCfg['isearch']['status'])?$this->jCfg['isearch']['status']:"all";
		$stat = ($mst=="all")?$this->status_form:"'".$mst."'";
		
		$this->per_page = $this->jCfg['isearch']['per_page'];
		$this->data_table = $this->A->data( array(
				"limit" 	=> $this->per_page, 
				"status"	=> $stat,
				"start_date"=> $this->jCfg['isearch']['start_date'],
				"end_date"	=> $this->jCfg['isearch']['end_date'],
				"offset" 	=> $this->uri->segment($this->uri_segment)
		) );
		
		$data = $this->_data(array(
				'base_url'		=> $this->own_link.'index'
			)
		);	
					
		$this->_v($this->folder_view.$this->prefix_view,$data);
	}
	
	function get_note($form_id="",$asuransi_id = ""){		
		$this->output->enable_profiler(FALSE);
		if(trim($form_id) !="" && trim($asuransi_id) != ""){
			$this->DATA->table = "tm_pialang_note";
			$m = $this->db->query("
				select * from tm_pialang_note where note_form_id = '".dbClean($form_id)."'
				AND note_as_id = '".dbClean($asuransi_id)."'
				order by note_id DESC
			")->row();
			if($this->jCfg['user']['id'] == $m->note_receiver){
				$this->DATA->_update(array(
					"note_form_id"		=> dbClean($form_id),
					"note_as_id"		=> dbClean($asuransi_id)
				),array(
					"note_read"	=> 1
				));
			}
			$mm = get_note(array(
				  	"form_id"		=> dbClean($form_id),
					"asuransi_id"	=> dbClean($asuransi_id)
				  ));
			$this->_v("note",array('data'=>$mm),FALSE);
		}else{
			echo "empty";
		}
	}
	
	function search(){
		$this->load->helper('url');		
		$this->uri_segment = 6;
			
		$column = ''; $keyword = '';				
		if( isset($_POST['column']) && isset($_POST['keyword']) ){
			$column = $_POST['column'];
			$keyword = url_title($_POST['keyword']);
			$this->setiSearch(array(
				"start_date"=> dbClean($_POST['dstart']),
				"end_date"	=> dbClean($_POST['dend']),
				"status"	=> dbClean($_POST['status']),
				"per_page"	=> dbClean($_POST['per_page'])
			));
		}else{
			$column =  $this->uri->segment(4);
			$keyword =   $this->uri->segment(5);
		}	
		
		$stat = ($this->jCfg['isearch']['status']=="all")?$this->status_form:"'".$this->jCfg['isearch']['status']."'";
		
		$this->per_page = $this->jCfg['isearch']['per_page'];
		$par = array(
						"column"	=> $column,
						"start_date"=> $this->jCfg['isearch']['start_date'],
						"end_date"	=> $this->jCfg['isearch']['end_date'],
						"status"	=> $stat,
						"keyword"	=> (trim($keyword)=="all")?"":$keyword
					) ;
		
		if(isset($_POST['cari'])){
			$par["limit"] 	= $this->per_page; 
			$par["offset"] 	= $this->uri->segment($this->uri_segment);	
		}
		
		$this->data_table = $this->A->data( $par );
		$k=($keyword=="")?"all":$keyword;
		$data = $this->_data(
			array(
				'base_url'		=>$this->own_link.'search/'.$column.'/'.$k
			)
		);	
		
		
		$this->header_page['key']=pTxt(($k=="all")?"":$k);
		$this->header_page['col']=$column;	
		$this->header_page['cat_search'] = array($column	=>	$this->cat_search);	
		if(isset($_POST['cari'])){
			$this->_v($this->folder_view.$this->prefix_view,$data);
		}elseif(isset($_POST['report'])){
			$this->_v($this->folder_view.$this->prefix_view."_html",$data,FALSE);
		}
	}
		
	function report($id=''){
		$id=dbClean(trim($id));		
		if(trim($id)!=''){
			
			$mdata = $this->A->data(array(
					'form_id'	=> $id
				));
			
			if(count($mdata['data'])==0)
				redirect($this->own_link);
				
			$this->data_form = $mdata['data'][0];
			$this->_v($this->folder_view.$this->prefix_view."_report",array(),FALSE);
		}else{
			redirect($this->own_link);
		}
	}	
}
